import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:ideology/features/screens/widgets/editor_menu_bar/setting/left/compact_current_sdk.dart';
import 'package:ideology/features/screens/widgets/editor_menu_bar/setting/left/installed_list.dart';
import 'package:ideology/features/screens/widgets/editor_menu_bar/setting/sdk_settings_controller.dart';

/// 左侧面板：用于显示和管理当前项目的 SDK 设置
/// 包含：
/// 1. 当前 SDK 信息（选择/复制/清除）
/// 2. 已安装 SDK 列表（切换/移除/文件夹定位）
/// 3. 添加新的本地 SDK
class BuildLeftPane extends StatelessWidget {
  const BuildLeftPane({
    super.key,
    required this.controller,
    required this.onSnack,
  });

  /// SDK 设置控制器，管理当前 SDK 状态和操作逻辑
  final SdkSettingsController controller;

  /// SnackBar 提示回调，用于显示操作反馈（如“路径已复制”）
  final void Function(String) onSnack;

  @override
  Widget build(BuildContext context) {
    final cs = Theme.of(context).colorScheme;

    /// 内部工具函数：生成一条浅色分割线
    Widget thinDivider() => Divider(
      thickness: 1,
      height: 1,
      color: cs.onSurface.withOpacity(0.1),
    );

    return Container(
      width: 260,
      padding: const EdgeInsets.all(12),
      decoration: BoxDecoration(
        border: Border(
          right: BorderSide(color: cs.onSurface.withOpacity(0.05)),
        ),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          /// ===== 当前 SDK 部分 =====
          Text('Project SDK', style: Theme.of(context).textTheme.titleSmall),
          const SizedBox(height: 8),

          /// 显示紧凑型的当前 SDK 信息卡片
          CompactCurrentSdk(
            currentPath: controller.data.currentSdkPath,
            onPickLocal: () =>
                controller.pickLocalSdk(context: context, onError: onSnack),
            onClear: () => controller.setCurrentSdkPath(''),
            onCopy: (p) {
              if (p != null && p.isNotEmpty) {
                Clipboard.setData(ClipboardData(text: p));
                onSnack('Path copied');
              } else {
                onSnack('No SDK selected');
              }
            },
          ),

          const SizedBox(height: 14),
          thinDivider(),
          const SizedBox(height: 12),

          /// ===== 已安装 SDK 列表 =====
          Text('Installed', style: Theme.of(context).textTheme.titleSmall),
          const SizedBox(height: 8),

          /// 展示所有已安装 SDK 的路径
          /// - 可以设为当前
          /// - 可以在文件管理器中打开
          /// - 可以移除
          Expanded(
            child: InstalledList(
              installed: controller.data.installedSdkPaths,
              current: controller.data.currentSdkPath,
              onMakeCurrent: controller.setCurrentSdkPath,
              onRevealInFinder: controller.revealInFileManager,
              onRemove: (p) async {
                // 弹出确认对话框
                final ok = await showDialog<bool>(
                  context: context,
                  builder: (c) => AlertDialog(
                    title: const Text('Remove installed SDK'),
                    content: Text('Remove from list?\n\n$p'),
                    actions: [
                      TextButton(
                        onPressed: () => Navigator.of(c).pop(false),
                        child: const Text('Cancel'),
                      ),
                      ElevatedButton(
                        onPressed: () => Navigator.of(c).pop(true),
                        child: const Text('Remove'),
                      ),
                    ],
                  ),
                );
                if (ok == true) controller.removeInstalled(p);
              },
            ),
          ),

          const SizedBox(height: 8),

          /// ===== 添加新的 SDK =====
          OutlinedButton.icon(
            onPressed: () =>
                controller.pickLocalSdk(context: context, onError: onSnack),
            icon: const Icon(Icons.add),
            label: const Text('Add Local SDK'),
            style: OutlinedButton.styleFrom(
              minimumSize: const Size.fromHeight(36),
              side: BorderSide.none,
            ),
          ),
        ],
      ),
    );
  }
}
